<?php
/**
**百度直达号
**请求接收地址
**Time 2015-11-04 pm 
**User by pzp
**/
include('/var/www/html/new/d/module/api/function.class.php');
include('/var/www/html/new/d/module/api/bd_zhidahao/baidu_zdh.class.php');

if (!class_exists("go_sql")){
	include("/var/www/html/new/conf/le.je");
	$le=new go_sql();
	$le->connect();
}

$BD_zdh = new BD_zdh();
$PFT_infunction = new AllFunction(); 

$BD_zdh->logfile(json_encode($_REQUEST),'bdzdh_get_data');

$secret_key = 'B3a702AcaE39525E050a6deB76F9f879';

// /* $txt = '{"msg":"{\"orderId\":200001902,\"customerName\":\"\\u5b8b\\u8def\\u79b9\",\"customerMobile\":\"18210114071\",\"citizenIdNumber\":\"410781199002161216\",\"subOrders\":[{\"subOrderId\":2533627344,\"itemNo\":\"113|2633|5715\",\"quantity\":\"1\",\"price\":\"1\",\"enableTime\":\"1446998400\",\"expireTime\":\"1447775999\"}]}","cmd":"order_add","timestamp":1447056456,"sign":"618ed51d5fe943c770fad49466d75a2a"}'; */
// $arr = json_decode($txt,true);
// print_r(json_decode($arr['msg'],true));exit;
$cmd = $_REQUEST['cmd'];
$msg = json_decode($_REQUEST['msg'],true);
$sign = $_REQUEST['sign'];
$timestamp = $_REQUEST['timestamp'];

$account = '504025';
$password = '5446f60483f3e3163189b84ff45054c6';

$checksign = md5('cmd='.$cmd.'msg='.$msg.'timestamp='.$timestamp.'secret_key='.$secret_key);

if($sign != $checksign){
	$BD_zdh->logfile("签名不通过",'bdzdh_sign_f');
	exit('{"error_code":"10072"}');
}
if($cmd == 'bd_product'){	//产品推送
	$get_product = $BD_zdh->Post_bd_Product();print_r($get_product);  //产品提交
}
elseif($cmd == 'order_add'){ //下单
	$orderId = $msg['orderId'];						//百度订单号
	$customerName = $msg['customerName'];			//用户姓名
	$customerMobile = $msg['customerMobile'];		//用户手机号
	$citizenIdNumber = $msg['citizenIdNumber'];		//用户身份证号
	// $subOrders = json_decode($msg['subOrders'],true);
	// $subOrder = json_decode($msg['subOrders'],true);			//子订单，每个子订单为一种商品的购买集合
	$subOrderId = $msg['subOrders'][0]['subOrderId'];		//百度子订单
	$itemNo =  explode('|',$msg['subOrders'][0]['itemNo']);//券商商品id
	$quantity =$msg['subOrders'][0]['quantity'];			//购买数量
	$price = $msg['subOrders'][0]['price'];				//商品单价
	$enableTime = $msg['subOrders'][0]['enableTime'];		//商品可使用的起始
	
	$submitO_array = array(
		'account'	=> $account ,
		'password'	=> $password ,
		'lid'		=> $itemNo[1] ,
		'tid'		=> $itemNo[2] ,
		'remoorder'	=> $orderId.'_'.$subOrder ,
		'tprice'	=> $price ,
		'num'		=> $quantity ,
		'playtime'	=> date('Y-m-d',$enableTime) , 
		'name'		=> $customerName ,
		'tel'		=> $customerMobile ,
		'sms'		=> 1 ,
		'paystatus'	=> 2 ,
		'tuanhao'	=> '' ,
		'aid'		=> $itemNo[0] ,
		'IDcard'	=> $citizenIdNumber 
	); 	
	
	$request = $PFT_infunction->outSide_Submit($submitO_array);
	$request_xml = simplexml_load_string($request);
	if((string)$request_xml->Rec->UUordernum){ //下单成功或者1075远端订单重复
		$ecodeDetail[] = (string)$request_xml->Rec->UUcode;
		$order[] = array(
			'subOrderId'	=> $subOrderId ,
			'subOrderNo'	=> (string)$request_xml->Rec->UUordernum ,
			'ecode'			=> (string)$request_xml->Rec->UUcode ,
			'qrUrl'			=> (string)$request_xml->Rec->UUqrcodeIMG ,
			'ecodeDetail'	=> $ecodeDetail
		);
		
		$orderNo = (string)$request_xml->Rec->UUordernum ; //取一个票付通的订单号作为主订单号
		$ecode = (string)$request_xml->Rec->UUcode; 
		$qrUrl  = (string)$request_xml->Rec->UUqrcodeIMG;
		
		$response_all = array(
			'orderId'	=> $orderId ,
			'orderNo'	=> $orderNo ,
			'ecode'		=> $ecode ,
			'qrUrl'		=> $qrUrl ,
			'subOrders'	=> $order
		);	
		$BD_zdh->logfile($orderId.'|'.$subOrderId.'|'.json_encode($order),'bdzdh_order_s');
		exit(json_encode($response_all));		
	} 
	else{
		$BD_zdh->logfile($orderId.'|'.$subOrderId.'|'.(string)$request_xml->Rec->UUerrorcode,'bdzdh_order_f');
		exit('{"error_code":"'.(string)$request_xml->Rec->UUerrorcode.'"}');
	}
}elseif($cmd == 'order_info'){ //查询订单

	$orderId = $msg['orderId'];						//百度订单号
	$subOrderss =  json_decode($msg['subOrders'],true);
	
	$pft_order_xml = $PFT_infunction->Select_REMOorder($orderId.'_'.$subOrderss[0]);
	if(strlen($pft_order_xml) < 47) exit('{"error_code":"10072"}');
	$ecodeDetail[] = (string)$pft_order_xml->Rec->UUcode;	
	$qrUrl = "http://12301.cc/".$PFT_infunction->url_sms((string)$pft_order_xml->Rec->UUordernum);	
	$order[] = array(
		'subOrderId'	=> $subOrderss[0] ,
		'subOrderNo'	=> (string)$pft_order_xml->Rec->UUordernum ,
		'ecode'			=> (string)$pft_order_xml->Rec->UUcode ,
		'qrUrl'			=> $qrUrl ,
		'ecodeDetail'	=> $ecodeDetail
	);
	$response_all = array(
		'orderId'	=> $orderId ,
		'orderNo'	=> (string)$pft_order_xml->Rec->UUordernum ,
		'ecode'		=> (string)$pft_order_xml->Rec->UUcode ,
		'qrUrl'		=> $qrUrl ,
		'subOrders'	=> $order
	);	
	$BD_zdh->logfile($orderId.'|'.json_encode($order),'bdzdh_orderinfo_s');
	exit(json_encode($response_all));
	
}elseif($cmd == 'check_list'){	//核销订单列表

	$orderId = $msg['orderId'];						//百度订单号
	$subOrderss =  json_decode($msg['subOrders'],true);
	
	$pft_order_xml = $PFT_infunction->Select_REMOorder($orderId.'_'.$subOrderss[0]);
	if(strlen($pft_order_xml) < 47) exit('{"error_code":"10072"}');	
	$UUstatus = (string)$pft_order_xml->Rec->UUstatus;
	$ecodeDetail[] = (string)$pft_order_xml->Rec->UUcode;
	$qrUrl = "http://12301.cc/".$PFT_infunction->url_sms((string)$pft_order_xml->Rec->UUordernum);
	if($UUstatus == 1){
		$checkList = array(
			'checkTime'	=> time((string)$pft_order_xml->Rec->UUdtime),
			'checkNum'	=> (int)$pft_order_xml->Rec->UUtnum,
			'ecodeList'	=> $ecodeDetail
		);
	}
	else $checkList = '';
	
	$order[] = array(
		'subOrderId'	=> $subOrderss[0] ,
		'subOrderNo'	=> (string)$pft_order_xml->Rec->UUordernum ,
		'checkList'		=> $checkList ,
	);
	
	$response_all = array(
		'orderId'	=> $orderId ,
		'orderNo'	=> (string)$pft_order_xml->Rec->UUordernum ,
		'subOrders'	=> $order
	);	
	$BD_zdh->logfile($orderId.'|'.json_encode($order),'bdzdh_ordercheck_s');
	exit(json_encode($response_all));
	
}elseif($cmd == 'refund_list'){	//退票订单列表
	
	$orderId = $msg['orderId'];						//百度订单号
	$subOrderss =  json_decode($msg['subOrders'],true);
	
	$pft_order_xml = $PFT_infunction->Select_REMOorder($orderId.'_'.$subOrderss[0]);
	if(strlen($pft_order_xml) < 47) exit('{"error_code":"10072"}');	
	$UUstatus = (string)$pft_order_xml->Rec->UUstatus;
	$ecodeDetail[] = (string)$pft_order_xml->Rec->UUcode;
	$qrUrl = "http://12301.cc/".$PFT_infunction->url_sms((string)$pft_order_xml->Rec->UUordernum);
	if($UUstatus == 3){
		$refundList = array(
			'refundTime'	=> time((string)$pft_order_xml->Rec->UUctime),
			'refundNum'	=> (int)$pft_order_xml->Rec->UUtnum,
			'ecodeList'	=> $ecodeDetail ,
			'status'	=> 2
		);
	}
	else $refundList = '';
	
	$order[] = array(
		'subOrderId'	=> $subOrderss[0] ,
		'subOrderNo'	=> (string)$pft_order_xml->Rec->UUordernum ,
		'refundList'	=> $refundList ,
	);
	
	$response_all = array(
		'orderId'	=> $orderId ,
		'orderNo'	=> (string)$pft_order_xml->Rec->UUordernum ,
		'subOrders'	=> $order
	);	
	$BD_zdh->logfile($orderId.'|'.json_encode($order),'bdzdh_orderrefund_s');
	exit(json_encode($response_all));
	
}elseif($cmd == 'unused_list'){	//未使用订单列表
	
	$orderId = $msg['orderId'];						//百度订单号
	$subOrderss =  json_decode($msg['subOrders'],true);
	
	$pft_order_xml = $PFT_infunction->Select_REMOorder($orderId.'_'.$subOrderss[0]);
	if(strlen($pft_order_xml) < 47) exit('{"error_code":"10072"}');	
	$UUstatus = (int)$pft_order_xml->Rec->UUstatus;
	$UUtnum = (int)$pft_order_xml->Rec->UUtnum;
	$ecodeDetail[] = (string)$pft_order_xml->Rec->UUcode;
	$qrUrl = "http://12301.cc/".$PFT_infunction->url_sms((string)$pft_order_xml->Rec->UUordernum);
	if($UUstatus == 0){
		$unusedList = array(
			'unusedNum'	=> $UUtnum,
			'ecodeList'	=> $ecodeDetail ,
		);
	}
	else $unusedList = '';
	
	$order[] = array(
		'subOrderId'	=> $subOrderss[0] ,
		'subOrderNo'	=> (string)$pft_order_xml->Rec->UUordernum ,
		'unusedList'	=> $unusedList ,
	);
	
	$response_all = array(
		'orderId'	=> $orderId ,
		'orderNo'	=> (string)$pft_order_xml->Rec->UUordernum ,
		'subOrders'	=> $order
	);	
	$BD_zdh->logfile($orderId.'|'.json_encode($order),'bdzdh_ordercheck_s');
	exit(json_encode($response_all));
	
}elseif($cmd == 'qr_image'){	//获取二维码图片内容

	$orderId = $msg['orderId'];						//百度订单号
	$subOrderss =  json_decode($msg['subOrders'],true);
	
	$pft_order_xml = $PFT_infunction->Select_REMOorder($orderId.'_'.$subOrderss[0]);
	if(strlen($pft_order_xml) < 47) exit('{"error_code":"10072"}');
	$qrUrl = "http://12301.cc/".$PFT_infunction->url_sms((string)$pft_order_xml->Rec->UUordernum);
	
	$response_all = array(
		'contentType'	=> 'image/jpeg' ,
		'raw'			=> base64_encode($qrUrl) ,
	);	
	$BD_zdh->logfile($orderId.'|'.json_encode($order),'bdzdh_orderqr_image_s');
	exit(json_encode($response_all));
	
}elseif($cmd == 'refund_apply'){	//退票申请

	$orderId = $msg['orderId'];						//百度订单号
	$auditId = $msg['auditId'];
	$subOrderss =  json_decode($msg['subOrders'],true);
	$refundNum = $subOrderss[0]['refundNum'];
	$pft_order_xml = $PFT_infunction->Select_REMOorder($orderId.'_'.$subOrderss[0]);
	if(strlen($pft_order_xml) < 47) exit('{"error_code":"10072"}');
	$pft_order = (string)$pft_order_xml->Rec->UUordernum ;
	$UUtnum = (int)$pft_order_xml->Rec->UUtnum;
	if($refundNum == $UUtnum){
		$cancel = $PFT_infunction->Cancel_Order($pft_order);
		$BD_zdh->logfile($orderId.'|'.$pft_order.'|'.$cancel,'bdzdh_orderrefund_apply_s');
		if($cancel == 100) $result = 1;
		else $result = $cancel;
		file_get_contents("http://www.12301.cc/module/api/bd_zhidahao/bazhd_orderNote.php?cmd=refund&auditId=$auditId&status=$cancel"); //退款通知
		exit('{"auditId":"'.$auditId.'","result":"'.$result.'"}');
	}
	else exit('{"error_code":"10072"}');
}

?>